<?php 
include("../inc/global.php");
include_once './menu_fun.php';

//note 排序提交表单
if( $_POST['ispost']){
	$sort = $_POST['sort'];
	foreach( $sort as $k=>$v ){
		$db->update('menu', array('sort'=>$v),'mid='.$k);
	}
	
	write_menu_cache();	
}

//note 删除菜单
if( $_GET['isdel'] ){
	$mid = $_GET['mid'];
	$num = $db->getCount('Mould','Menu_id='.$mid);
	if( $num ){
		echo '<script type="text/javascript">alert("删除失败，该菜单下的模块不为空！");history.back();</script>';	
	}else{
		$db->delete('menu','mid='.$mid);
		
		write_menu_cache();	
	}
}

//note 取得所有显示位置，即一二级菜单
$sql = "SELECT * FROM menu ORDER BY sort ASC";
$menu = $db->getAll( $sql );

$menu_arr = array();
foreach( $menu as $v ){
	$menu_arr[$v['pid']][$v['mid']] = $v;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CMS</title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="./frame/js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="../styles/main.css" />
</head>
<body>
<form method="post">
<table width="90%" cellpadding="1" cellspacing="1" class="list-div" style="font-size:12px;">
	<tr>
		<td width="80">排序</td>
		<td>名称</td>
        <td width="40" align="center">操作</td>
	</tr>
    <?php foreach( $menu_arr[0] as $v){?>
    <tr>
		<td><input type="text" name="sort[<?php echo $v['mid'];?>]" style="width:26px" value="<?php echo $v['sort'];?>"/></td>
		<td id="name_<?php echo $v['mid'];?>" onClick="edit(<?php echo $v['mid'];?>)"><?php echo $v['name'];?></td>
        <td align="center"><a href="javascript:del(<?php echo $v['mid'];?>);">删除</a></td>
	</tr>
		<?php if( !empty($menu_arr[$v['mid']]) ){ foreach( $menu_arr[$v['mid']] as $v2){?>
          <tr>
            <td style="padding-left:30px;"><input type="text" style="width:26px" name="sort[<?php echo $v2['mid'];?>]" value="<?php echo $v2['sort'];?>"/></td>
            <td style="padding-left:30px;" id="name_<?php echo $v2['mid'];?>" onClick="edit(<?php echo $v2['mid'];?>)"><?php echo $v2['name'];?></td>
            <td align="center"><a href="javascript:del(<?php echo $v2['mid'];?>);">删除</a></td>
        </tr>
    <?php }}}?>
</table>
    <input name="dosubmit" type="submit" value=" 提 交 " id="dosubmit" />
    <input type="hidden" name="ispost" value="1" />
</form>
<script type="text/javascript">
function del( mid ){
	var ret = window.confirm("您确定要删除该菜单吗？");
	if( ret ){
		document.location.href = document.location.href + '?isdel=1&mid='+mid;	
	}	
}

var editing = false;
function edit( mid ){
	if( editing ) return;
	var name = $("#name_"+mid).html();
	$("#name_"+mid).html('<input id="t_'+mid+'" type="text" value="'+name+'" onblur="send('+mid+',this.value)" />');	
	$("#t_"+mid).focus();
	editing = true;
}

function send( mid, name ){
	$("#name_"+mid).html( name );
	editing = false;
	$.get('./ajax.php?m=menu_edit&mid='+mid+'&name='+name,function(data){
		if( parseInt(data) ){
			alert("编辑成功！");	
		}else{
			alert("编辑失败！")	
		}
	});
}
</script>
</body>
</html>